package com.gongpingjialoan.activity.car;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 预约成功
 */
public class SubscribeSucceedActivity extends BaseActivity implements View.OnClickListener {
    private JSONObject shareJo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_succeed);

        initView();
    }

    private void initView() {
        setTitle("预约成功");

        findViewById(R.id.gohome).setOnClickListener(this);
        findViewById(R.id.share).setOnClickListener(this);

        try {
            shareJo = new JSONObject(getIntent().getStringExtra("share"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        Intent it;
        switch (v.getId()) {
            case R.id.gohome:
                it = new Intent(SubscribeSucceedActivity.this, MainActivity.class);
                startActivity(it);
                break;
            case R.id.share:
                share();
                break;
            default:
                break;
        }
    }

    private void share() {
        if (shareJo != null) {
            try {
                String url = shareJo.getString("link");
                String title = shareJo.getString("title");
                String content = shareJo.getString("desc");
                UMImage image = new UMImage(SubscribeSucceedActivity.this, shareJo.getString("image_url"));
                new ShareAction(this).setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                        .withTitle(title)
                        .withMedia(image)
                        .withText(content)
                        .withTargetUrl(url)
                        .setCallback(umShareListener)
                        .open();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {

            Toast.makeText(SubscribeSucceedActivity.this, "分享成功!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(SubscribeSucceedActivity.this, " 分享失败!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(SubscribeSucceedActivity.this, "分享取消!", Toast.LENGTH_SHORT).show();
        }
    };
}
